
import store from '../store/index'
export const BASEURL = 'https://bifang.rzkeji.com/api';
export function getData (url,data) {
  var url=BASEURL+url
  let promise=new Promise((resolve ,reject) => {
    wx.request({
      url:url,
      data:data,
      method:"GET",
      header:{
        "content-type":"application/json"
      },
      success:res =>{
        if(res.data.data=='no_login'){
        //  const url= `/${getCurrentPages()[0].route}`
        //   store.dispatch('setUrl',url)      
          wx.switchTab({
            url:"/pages/me/main"
          })
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }else if(res.data.data=='no_binding'){
          wx.switchTab({
            url:"/pages/me/main"
          })
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }else{
          resolve(res)
        }
        
      },
      fail:err =>{
        reject(err)
      }
    })
  })
  return promise;
};
export function postData (url,data) {
  var url=BASEURL+url
  let promise=new Promise((resolve ,reject) => {
    wx.request({
      url:url,
      data:data,
      method:"POST",
      header:{
        "content-type":"application/json"
      },
      success:res =>{
        if(res.data.data=='no_login'){
          wx.switchTab({
            url:"/pages/me/main"
          })
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }else if(res.data.data=='no_binding'){
          wx.switchTab({
            url:"/pages/me/main"
          })
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }else{
          resolve(res)
        }
      },
      fail:err =>{
        reject(err)
      }
    })
  })
  return promise;
}

export default {
  getData,
  postData,
  BASEURL
}
